package org.poptest.populate.provider.impl;

import org.poptest.populate.provider.AbstractRangeProvider;

import java.util.Arrays;

/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @author Alan Roche
 *         Date: 15/02/11
 *         Time: 22:08
 */
public class CharProvider extends AbstractRangeProvider<Character> {
    private int index = 0;
    private static final char[] CHARACTERS = new char[] {
            'a','b','c', 'd', 'e' , 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'O'  , 'P' , 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public CharProvider() {
        super(Character.class, CHARACTERS[0], CHARACTERS[CHARACTERS.length - 1]);
    }


    @Override
    protected Character doNextValue() {
        if (value == CHARACTERS[0]) {
            index = 0;
        }
        return CHARACTERS[++index];
    }

    public void setValue(Character value) {
        super.value = value;
        index = Arrays.binarySearch(CHARACTERS, value);
    }
}
